<template>
  <div class="coach-rating-list">
    <el-card class="rating-card">
      <template #header>
        <div class="card-header">
          <span>教练评分管理</span>
          <div class="header-actions">
            <el-input
              v-model="searchForm.keyword"
              placeholder="请输入教练姓名"
              style="width: 300px; margin-right: 10px"
              clearable
              :maxlength="200"
              @clear="handleSearch"
              @keydown.enter="handleSearch"
            >
              <template #append>
                <el-button icon="Search" @click="handleSearch" :loading="loading" />
              </template>
            </el-input>
          </div>
        </div>
      </template>

      <el-table
        :data="ratingList"
        v-loading="loading"
        style="width: 100%"
        row-key="id"
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="coach.name" label="教练姓名" show-overflow-tooltip />
        <el-table-column prop="user.name" label="评分用户" width="120" />
        <el-table-column prop="overallRating" label="综合评分" width="100">
          <template #default="scope">
            <el-rate
              v-model="scope.row.overallRating"
              disabled
              show-score
              text-color="#ff9900"
              score-template="{value}分"
            />
          </template>
        </el-table-column>
        <el-table-column prop="professionalism" label="专业度" width="80" />
        <el-table-column prop="patience" label="耐心度" width="80" />
        <el-table-column prop="enthusiasm" label="热情度" width="80" />
        <el-table-column prop="communication" label="沟通能力" width="100" />
        <el-table-column prop="content" label="评价内容" show-overflow-tooltip />
        <el-table-column prop="createdAt" label="评分时间" width="180" />
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="scope">
            <el-button size="small" @click="handleView(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 查看评分详情对话框 -->
    <el-dialog
      v-model="detailDialogVisible"
      title="评分详情"
      width="800px"
    >
      <div class="rating-detail">
        <el-descriptions :column="1" border>
          <el-descriptions-item label="教练ID">{{ currentRating.coachId }}</el-descriptions-item>
          <el-descriptions-item label="用户">{{ currentRating.user?.name }}</el-descriptions-item>
          <el-descriptions-item label="综合评分">
            <el-rate
              v-model="currentRating.overallRating"
              disabled
              show-score
              text-color="#ff9900"
              score-template="{value}分"
            />
          </el-descriptions-item>
          <el-descriptions-item label="专业度">{{ currentRating.professionalism }}</el-descriptions-item>
          <el-descriptions-item label="耐心度">{{ currentRating.patience }}</el-descriptions-item>
          <el-descriptions-item label="热情度">{{ currentRating.enthusiasm }}</el-descriptions-item>
          <el-descriptions-item label="沟通能力">{{ currentRating.communication }}</el-descriptions-item>
          <el-descriptions-item label="内容">{{ currentRating.content }}</el-descriptions-item>
          <el-descriptions-item label="匿名">{{ currentRating.isAnonymous ? '是' : '否' }}</el-descriptions-item>
          <el-descriptions-item label="评分时间">{{ currentRating.createdAt }}</el-descriptions-item>
        </el-descriptions>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="detailDialogVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import type { CoachRating } from '@/types/api/review'
import { coachRatingApi } from '@/api/modules/review'

// 表格相关
const loading = ref(false)
const ratingList = ref<CoachRating[]>([])
const currentRating = ref<CoachRating>({
  id: 0,
  coachId: 0,
  userId: 0,
  user: {
    id: 0,
    name: '',
    avatar: ''
  },
  overallRating: 0,
  content: '',
  isAnonymous: false,
  createdAt: ''
})

// 搜索表单
const searchForm = reactive({
  keyword: ''
})

// 分页相关
const pagination = reactive({
  page: 1,
  pageSize: 10,
  total: 0
})

// 对话框相关
const detailDialogVisible = ref(false)

// 获取评分列表
const getRatingList = async () => {
  try {
    loading.value = true
    // 这里需要根据实际接口调整，暂时使用模拟数据
    // const res = await coachRatingApi.getCoachRating(coachId)

    // 模拟数据
    ratingList.value = [
      {
        id: 1,
        coachId: 1,
        userId: 1,
        user: {
          id: 1,
          name: '张三',
          avatar: ''
        },
        overallRating: 4.8,
        professionalism: 5,
        patience: 5,
        enthusiasm: 5,
        communication: 4,
        content: '教练非常专业，讲解清晰！',
        isAnonymous: false,
        createdAt: '2025-10-25T18:30:00Z'
      }
    ]
    pagination.total = 1
  } catch (error) {
    ElMessage.error('获取评分列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  pagination.page = 1
  getRatingList()
}

// 刷新列表
const refreshList = () => {
  getRatingList()
}

// 查看评分详情
const handleView = (row: CoachRating) => {
  currentRating.value = row
  detailDialogVisible.value = true
}

// 分页相关方法
const handleSizeChange = (val: number) => {
  pagination.pageSize = val
  getRatingList()
}

const handleCurrentChange = (val: number) => {
  pagination.page = val
  getRatingList()
}

// 生命周期
onMounted(() => {
  getRatingList()
})
</script>

<style scoped>
.coach-rating-list {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
